package com.foryou.system.modules.wx.controller;

import com.alibaba.fastjson.JSONObject;
import com.foryou.system.common.bootstrap.PageUtils;
import com.foryou.system.common.bootstrap.Query;
import com.foryou.system.common.utils.R;
import com.foryou.system.modules.wx.entity.PlaceItem;
import com.foryou.system.modules.wx.entity.ShootItem;
import com.foryou.system.modules.wx.service.PlaceService;
import com.foryou.system.modules.wx.service.ShootService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Lee.
 */
@RestController
@RequestMapping("/wx/shoot")
public class ShootController {

    @Autowired
    private ShootService shootService;

    @Autowired
    private RestTemplate restTemplate;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        int total = shootService.queryTotal(query);
        List<ShootItem> sysLogList = shootService.queryList(query);

        return new PageUtils(sysLogList, total);
    }

    @RequestMapping("/getDetail/{id}")
    public R getDetail(@PathVariable("id") Long id,HttpServletRequest httpRequest) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("token",httpRequest.getHeader("token"));
        JSONObject jsonObject = restTemplate.postForObject("http://wechat/v/shoot/getForShootDetail/"+ id,new HttpEntity<String>(headers), JSONObject.class);
        return R.ok(jsonObject);
    }

    @RequestMapping("/update")
    public R update(@RequestBody Map<String, Object> params,HttpServletRequest httpRequest) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("token",httpRequest.getHeader("token"));
        if (shootService.update(params) > 0) {
            try {
                restTemplate.postForObject("http://wechat/v/wx/pushUserShootHandler/"+ Long.valueOf(params.get("id").toString()),new HttpEntity<String>(headers), JSONObject.class);
            } catch (RuntimeException e) {

            }
            return R.ok();
        }
        return R.error();
    }
}
